/**
 * 页面上的一些实用函数
 */
PageTools = {
	setGuidePageTableHeight: function() {
		var width = ($(document).width() - 24) / 3;
		$('.guide-page-table img').height(width);
		if (!$('body').data('.guide-page-table.resize')) {
			$(window).resize(function() {
				$('body').data('.guide-page-table.resize', true);
				var width = ($(document).width() - 24) / 3;
				$('.guide-page-table img').height(width);
			});
		}
	},
	/**
	 * 触发本地输入
	 * @param inputEl
	 */
	triggerFileInputTag: function(inputEl, params) {
		window.setTimeout(function() {
			PageTools.nativeFile(inputEl, params);
	    }, 0);
	},
	/**
	 * 触发本地输入
	 * @param inputEl params
	 */
	nativeFile: function(inputEl, params) {
	    if (!inputEl.value) {
	        return;
	    }
	    var fileObj = inputEl.files[0];
	    if (!fileObj) {
	        return;
	    }
	    var URLApi = window.URL || window.webkitURL;
	    if (URLApi) {
	        var blobURL = URLApi.createObjectURL(fileObj);
	        if (blobURL) {
	        	params.url = blobURL;
	        	this.loadPicture(params);
	        } else {
	            console.log('URL.createObjectURL returned null');
	        }
	    } else {
	    	console.log('URL.createObjectURL() not supported.');
	    }
	},
	/**
	 * 加载图片
	 * @param params
	 * {
	 * url: '',//图片的地址
	 * imgId: '',//图片的id
	 * insertSelector: ''//插入的元素选择器
	 * removeCallback: ''//删除时的回调函数
	 * }
	 */
	loadPicture: function(params) {
		var url = params.url;
		try {
			window.atob(url);
			url = "data:image/jpeg;base64," + url;// if we got here it is a base64 string (DATA_URL)
		} catch (e) {
		    console.log('Exception URL: ' + url.slice(0, 100));// not DATA_URL
		}
	    var id = params.imgId;//'img' + new Date().getTime();
	    var startTime = new Date();
	    var html = ['<div class="form-group" style="margin:3px;border:2px solid green;">',
	    	'<div style="border-bottom:2px solid green;">',
	    	'<a href="javascript:void(0)" id="',
	    	'rotate',
	    	id,
	    	'" style="margin:3px;display:inline-block;padding:3px;">旋转图片</a>',
	    	'<a href="javascript:void(0)" id="',
	    	'remove',
	    	id,
	    	'" style="margin:3px;display:inline-block;padding:3px;">删除图片</a>',
	    	'</div>',
	    	'<div class="loadpicdiv" style="overflow:hidden;">',
	    	'<img id="',
	    	id,
	    	'" alt="加载中..." src="" >',
	    	'</div>',
	    	'</div>'
	    	].join('');
	    $(html).insertBefore(params.insertSelector);
	    window.setTimeout(function() {
	        $('#remove' + id).bind('click', function(e) {
	        	e.preventDefault();
	        	$(e.target).closest('div.form-group').remove();
	        	if (params.removeCallback && $.isFunction(params.removeCallback)) {
	        		params.removeCallback();
	        	}
	        	$(e.target).blur();
	        });
	    });
	    var img = document.getElementById(id);
	    img.src = url;
	    img.onloadend = function() {
	    	URLApi.revokeObjectURL(url);
	    };
	    window.setTimeout(function() {
	    	PageTools.displayImageUsingCanvas(id);
	    }, 0);
		console.log('setPicture load time: ' + (new Date() - startTime));
	},
	displayImageUsingCanvas: function(imgid) {
		var cid = 'canvas_' + imgid;
	    var img = document.getElementById(imgid);
		var width = $(img).closest('div.form-group').width();//取比例
		if (!width || width <= 100) {
			width = 200;
		}
	    Global.loadImage(img, function() {
			Global.loading();
	        img.parentNode.appendChild(Global.rotateImage({
				imageId: imgid,
				canvasId: cid,
				width: width
			}));
	        img.style.display = 'none';
	        Global.loaded();
	    });
		var imageType = Global.imageType(img);
		$('#rotate' + imgid).bind('click', function(e) {
			e.preventDefault();
			Global.loading();
			var cvs = Global.rotateImage({
				imageId: imgid,
				canvasId: cid,
				width: width
			});
	        Global.loaded();
		});
	},
	logout: function() {
		$.ajax({
			type: 'GET',
	    	url: GLOBAL_CONTEXT_PATH + '/login/logoutjson.html?_=' + new Date().getTime(),
	    	dataType: 'json',
	    	success: function (data) {
    			Global.loginInfo = {};
    			Global.setItem(Global.loginKey, Global.loginInfo);
	    		window.location.hash = '#userlogin';
	    	}
		});
	}
};
jQuery.fn.swapElement = function(to) {
    return this.each(function() {
        var copy_to = $(to).clone(true);
        $(to).replaceWith(this);
        $(this).replaceWith(copy_to);
    });
};